<template>
  <el-dialog
    :title="title"
    v-model="visible"
    width="900px"
    @close="onClose"
    :close-on-click-modal="false"
  >
    <div class="main">
      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="用户名称" prop="userName">
          <el-input
            style="width: 200px"
            v-model.trim="queryParams.userName"
            placeholder="请输入用户名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input
            style="width: 200px"
            v-model.trim="queryParams.mobile"
            placeholder="请输入手机号"
            clearable
            maxlength="11"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button plain type="primary" @click="handleExport">导出数据</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="state.loading" size="default" index :data="dataList">
        <el-table-column type="index" :index="indexAdd" label="序号" width="50" align="center" />
        <el-table-column label="用户名称" show-overflow-tooltip align="center" prop="nickName" />
        <el-table-column label="手机号" show-overflow-tooltip align="center" prop="userMobile" />
      </el-table>
      <div style="margin-right: 10px">
        <pagination
          v-show="state.total > 0"
          :total="state.total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import {
  getItemNewUserListApi,
  getItemOrderUserListApi,
  getItemTotalUserListApi
} from '@/api/statistic'
const queryRef = ref(null)

const dataList = ref([])
const visible = ref(false)
const title = ref('')

const queryParams = reactive({
  positionId: '',
  userName: '',
  mobile: '',
  startDate: '',
  endDate: '',
  pageNum: 1,
  pageSize: 10
})
const state = reactive({
  total: 0,
  loading: false
})

/** 查询用户列表 */
async function getList() {
  state.loading = true

  try {
    let r = {}
    if (title.value === '新用户数') {
      r = await getItemNewUserListApi(queryParams)
    }
    if (title.value === '累计用户数') {
      r = await getItemTotalUserListApi(queryParams)
    }
    if (title.value === '下单用户数') {
      r = await getItemOrderUserListApi(queryParams)
    }
    dataList.value = r?.data?.rows ?? []
    state.total = r?.data?.total ?? 0
  } finally {
    state.loading = false
  }
}

// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置
function resetQuery() {
  queryRef.value.resetFields()
  handleQuery()
}

function onClose() {
  queryRef.value.resetFields()
  queryParams.pageNum = 1
}

/** 导出按钮操作 */
const { proxy } = getCurrentInstance()
function handleExport() {
  let url = ''
  switch (title.value) {
    case '累计用户数':
      url = 'operation/statistics/admin/user-layer/total-user/export'
      break
    case '下单用户数':
      url = 'operation/statistics/admin/user-layer/order-user/export'
      break
    case '新用户数':
      url = 'operation/statistics/admin/user-layer/new-user/export'
      break
    default:
      break
  }
  proxy.download(url, queryParams, `${title.value}${new Date().getTime()}.xlsx`)
}

// 表格编号递增
function indexAdd(index) {
  const { pageNum, pageSize } = queryParams
  return index + 1 + (pageNum - 1) * pageSize
}

function init(row, name, params) {
  visible.value = true
  queryParams.positionId = row.positionId
  queryParams.startDate = params.startDate
  queryParams.endDate = params.endDate
  console.log('params', params)
  queryParams.machineId = row.machineSn
  title.value = name
  getList()
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped>
:deep(.pagination-container .el-pagination) {
  right: 20px;
}
.type {
  margin-bottom: 20px;
}
</style>
